var myMap;
var pointsShown;
var placemarks;
var addingPlacemark;

ymaps.ready(init);

function init () {
	initMap();
	initPlacemarks();
    
	if (document.title == "Points adding page")
		checkData();    
	
	// Mouse icon should be pointer always
    document.getElementsByClassName("ymaps-glass-pane ymaps-events-pane").
	item(0).addEventListener('mouseup', function(e) {
		this.style.cursor = "pointer";
	}, false);
    
    // Setting id of deleting point
    myMap.geoObjects.each(function (geoObject) {
		geoObject.events.add('click', function (e) {
			var coords = geoObject.geometry.getCoordinates();
			document.getElementById("deleteId").setAttribute("value", geoObject.id);
		    console.log(coords);
	    });
	});	
}

function onClick(e) {
    if (addingPlacemark != null) 
    	myMap.geoObjects.remove(addingPlacemark);

    var coords = e.get('coordPosition');
    
    document.getElementById("latInput").setAttribute("value", coords[0]);
    document.getElementById("lonInput").setAttribute("value", coords[1]);

    addingPlacemark = new ymaps.Placemark([coords[0], coords[1]], {
            hintContent: 'Сюда'
        }, {
            iconImageHref: 'http://api.yandex.ru/maps/doc/jsapi/2.x/examples/images/myIcon.gif',
            iconImageSize: [30, 42],
        }
    );
    myMap.geoObjects.add(addingPlacemark);
    checkLon(e);
	checkLat(e);
}

function initMap() {
	var mapDiv = document.getElementById("map");
	mapDiv.style.width = (screen.width - 40)/2 + "px";

    myMap = new ymaps.Map("map", {
        center: [52.089261, 23.698397],
        zoom: 15
    });
    
    myMap.controls
	    .add('zoomControl', { left: 5, top: 5 })
	    .add('typeSelector');
    
    myMap.events.add('click', onClick);
    document.getElementById("map").style.cursor = "pointer";   
}

function initPlacemarks() {
	placemarks = new Array();
	var lat, lon, name, id;

	var count = document.getElementById("count").innerHTML;
	
	for (var i = 0; i < count; ++i) {
		id = document.getElementById((i+1) + "id").innerText;
	    lat = document.getElementById((i+1) + "lat").innerText;
	    lon = document.getElementById((i+1) + "lon").innerText;
	    name = document.getElementById((i+1) + "name").innerText;

	    placemarks[i] = new ymaps.Placemark([lat, lon], {
	        balloonContentHeader: name
	    });
	    placemarks[i].id = id;
	    
	    myMap.geoObjects.add(placemarks[i]);
	}	

	pointsShown = true;
}

function showPoints() {
	var len = placemarks.length;
	if (!pointsShown) {		
		for (var i = 0; i < len; ++i) 
	        myMap.geoObjects.add(placemarks[i]);
	    pointsShown = true;
	    document.getElementById("showhide").setAttribute("value", "Hide points");
	} else {
		for (var i = 0; i < len; ++i) 
	        myMap.geoObjects.remove(placemarks[i]);
	    pointsShown = false;
	    document.getElementById("showhide").setAttribute("value", "Show points");
	}
}

function checkName(e) {
	var name = document.getElementById("nameInput");

	if (name.value.trim() == "") {
		name.style.border = "1px solid red";
		return false;
	}		
	else {
		name.style.border = "1px solid green";
		return true;
	}	
}

function checkType(e) {
	var type = document.getElementById("typeInput");

	if (type.value.trim() == "") {
		type.style.border = "1px solid red";
		return false;
	} else {
		type.style.border = "1px solid green";
		return true;
	}		
}

function checkLon(e) {
	var lon = document.getElementById("lonInput");
	
	if (isNaN(parseFloat(lon.value.trim()))) {
		lon.style.border = "1px solid red";
		return false;
	} else {
		lon.style.border = "1px solid green";
		return true;
	}	
}

function checkLat(e) {
	var lat = document.getElementById("latInput");
	
	if (isNaN(parseFloat(lat.value.trim()))) {
		lat.style.border = "1px solid red";
		return false;
	} else {
		lat.style.border = "1px solid green";
		return true;
	}
}

function checkData(e) {
	var r1 = checkName(e), r2 = checkType(e),
		r3 = checkLon(e), r4 = checkLat(e);
	return r1 && r2 && r3 && r4;
}




